package com.zlx.sharelive.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.LoginCodeBean;
import com.zlx.module_base.base_api.res_data.UpdatePasswordBean;
import com.zlx.module_base.base_api.res_data.WalletPasswordBody;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.utils.StringUt;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 支付设置
 */
public class PayAc extends BaseAc {

    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.et_code)
    EditText et_code;
    @BindView(R.id.et_pass1)
    EditText et_pass1;
    @BindView(R.id.et_pass2)
    EditText et_pass2;
    @BindView(R.id.tv_code)
    TextView tv_code;
    @BindView(R.id.tv_area_code)
    TextView tv_area_code;

    private TimeCount time;
    private String smsType = "3";

    @Override
    protected int getLayoutId() {
        return R.layout.ac_pass;
    }

    @Override
    public void initViews() {
        super.initViews();

        time = new TimeCount(60000, 1000);
    }

    @OnClick({R.id.iv_back, R.id.tv_code, R.id.tv_comm, R.id.ll_area_code})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.ll_area_code:
                //区号
                startActivityForResult(new Intent(this, AreaCodeListAc.class), 20);
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_code:
                if (StringUt.isEmpty(et_phone.getText().toString().trim())) {
                    Toast.makeText(this, "手机号不可为空", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    tv_code.setEnabled(true);
                    getCode();
                }
                break;
            case R.id.tv_comm:
                if (StringUt.isEmpty(et_phone.getText().toString().trim())) {
                    Toast.makeText(this, "手机号不可为空", Toast.LENGTH_SHORT).show();
                    return;
                } else if (StringUt.isEmpty(et_code.getText().toString().trim())) {
                    Toast.makeText(this, "验证码不可为空", Toast.LENGTH_SHORT).show();
                    return;
                } else if (et_pass1.getText().toString().trim().length() != 6) {
                    Toast.makeText(this, "请输入6位密码", Toast.LENGTH_SHORT).show();
                    return;
                } else if (et_pass2.getText().toString().trim().length() != 6) {
                    Toast.makeText(this, "请输入6位密码", Toast.LENGTH_SHORT).show();
                    return;
                } else if (!et_pass1.getText().toString().trim().equals(
                        et_pass2.getText().toString().trim())) {
                    Toast.makeText(this, getResources().getString(R.string.the_two_passwords_do_not_match), Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    updatePassword();
                }
                break;
        }
    }

    /**
     * 修改密码
     */
    private void updatePassword() {
        WalletPasswordBody walletPasswordBody = new WalletPasswordBody();
        walletPasswordBody.setMobile((tv_area_code.getText().toString().trim() + et_phone.getText().toString().trim()).replace("+", ""));
        walletPasswordBody.setCode(et_code.getText().toString().trim());
        walletPasswordBody.setLoginPwd(et_pass1.getText().toString().trim());
        walletPasswordBody.setConfirmPwd(et_pass2.getText().toString().trim());
        ApiUtil.getLoginApi().setWalletPassword(
                SPUtil.getToken(this),
                walletPasswordBody).observe(this,
                new BaseObserver<>(new BaseObserverCallBack<ApiResponse<UpdatePasswordBean>>() {
                    @Override
                    public void onSuccess(ApiResponse<UpdatePasswordBean> data) {
                        if (null != data) {
                            if (null != data.getData()) {
                                if (data.getData().getCode() == 0) {
                                    Toast.makeText(PayAc.this, "修改成功", Toast.LENGTH_SHORT).show();
                                    finish();
                                } else {
                                    Toast.makeText(PayAc.this, data.getData().getMsg(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();

                    }
                }));
    }

    /**
     * 获取验证码
     */
    private void getCode() {
        ApiUtil.getLoginApi().login_code((tv_area_code.getText().toString().trim() + et_phone.getText().toString().trim()).replace("+", ""), smsType)
                .observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<LoginCodeBean>>() {
                    @Override
                    public void onSuccess(ApiResponse<LoginCodeBean> data) {
                        if (null != data) {
                            if (null != data.getData()) {
                                if (data.getData().getCode() == 1002) {
                                    time.start();
                                } else {
                                    Toast.makeText(PayAc.this, data.getData().getMsg(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();

                    }
                }));
    }

    public class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tv_code.setTextColor(Color.parseColor("#666666"));
            tv_code.setClickable(false);
            tv_code.setText("(" + millisUntilFinished / 1000 + ") s");
        }

        @Override
        public void onFinish() {
            tv_code.setText(getResources().getText(R.string.resend_code));
            tv_code.setClickable(true);
            tv_code.setTextColor(Color.parseColor("#FF64B4"));
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (null == data) {
            return;
        }
        if (resultCode == 30) {
            tv_area_code.setText(data.getStringExtra("code"));
        }
    }
}
